<?php

namespace App\Repositories;

use App\Http\Helper\BeanHelper;
use App\Models\TestingQuestion;
use DB;

class TestingQuestionRepository extends Repository
{
    static $tag = 'testing.question';

    public function store($data) {
        $this->clearCache(self::$tag);
        $res = TestingQuestion::firstOrCreate($data);
        return $res;
    }

    /**
     * 根据id获取题目信息
     * @param $id
     * @return mixed
     */
    public function getOneById($id) {
        $cacheKey = getCacheKey('testing.cache_key.testing.question') . $id;
        $result = $this->remember($cacheKey, function () use ($id) {
            return BeanHelper::convertStdClsToArr(TestingQuestion::where('id', $id)->first());
        });
        return $result;
    }

    /**
     * 根据ID 获取题目列表，根据参数id排序
     * @param array $ids
     * @return mixed
     */
    public function getListByIdsOrderByParam($ids) {
        $cacheKey = getCacheKey('testing.cache_key.testing.question_list') . json_encode($ids);
        $result = $this->remember($cacheKey, function () use ($ids) {
            $list = TestingQuestion::whereIn('id', $ids)
                ->orderBy(DB::raw('FIELD(id,' . implode(',', $ids) . ')'), 'ASC')
                ->get();
            return BeanHelper::convertStdClsToArr($list);
        });
        return $result;
    }

    public function model() {
        return app(TestingQuestion::class);
    }

    public function tag() {
        return self::$tag;
    }
}